package cn.lgs.com.mobileplayer.http;

import rx.Observable;
import rx.Subscriber;

/**
 * Created by lgs on 2017/12/10.
 * 统一的数据剥离
 */

public class RxHelper {

    /**
     * 用来统一处理Http的resultCode,并将返回的Data部分剥离出来返回给subscriber
     *
     * @param <T> Subscriber真正需要的数据类型,也就是Data部分的数据类型
     * @return
     */
    /*public static <T> Observable.Transformer<BaseEntity<T>, T> handleReuslt() {
        return new Observable.Transformer<BaseEntity<T>, T>() {
            @Override
            public Observable<T> call(Observable<BaseEntity<T>> tObservable) {
                return tObservable.flatMap(new Func1<BaseEntity<T>, Observable<?>>() {
                    @Override
                    public Observable<T> call(BaseEntity<T> result) {
                        if (result.getRet() == 1) {
                            return createData(result.getData());
                        }
                        return null;
                    }
                }).subscribeOn(Schedulers.io())
                        ;
            }
        };
    }*/

    private static <T> Observable<T> createData(final T data) {
        return Observable.create(new Observable.OnSubscribe<T>() {

            @Override
            public void call(Subscriber<? super T> subscriber) {
                try {
                    subscriber.onNext(data);
                } catch (Exception e) {
                    subscriber.onError(e);
                }
            }
        });
    }
}
